<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" >
      <el-form-item label="试题名称" prop="examName">
        <el-input v-model="queryParams.examName" placeholder="请输入试题名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-input v-model="queryParams.year" placeholder="请输入年份" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:post:add']">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="试题名称" align="center" prop="examName" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="满分" align="center" prop="score" />
      <el-table-column label="试卷" align="center" width="120">
        <template #default="scope">
          <el-button type="primary" @click="viewQuestion(scope.row)">点击查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="答案" align="center" width="120">
        <template #default="scope">
          <el-button type="primary" @click="viewAnswer(scope.row)">点击查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 新增试题 -->
    <el-dialog title="新增试题" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="试题名称" prop="examName">
          <el-input v-model="form.examName" placeholder="请输入试题名称" />
        </el-form-item>
        <el-form-item label="分数" prop="score">
          <el-input-number v-model="form.score" placeholder="满分" :min="1" />
        </el-form-item>
        <el-form-item label="试题资源" prop="questionUrl">
          <el-input v-model="form.questionUrl" placeholder="请输入试题资源地址" />
        </el-form-item>
        <el-form-item label="答案资源" prop="answerUrl">
          <el-input v-model="form.answerUrl" placeholder="请输入答案资源地址" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="open = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup name="Exam">
import Cookies from "js-cookie";
import { reactive, toRefs } from "vue";
import { createOne, queryList, deleteOne } from "@/api/exam";
const { proxy } = getCurrentInstance();
const router = useRouter();
const loading = ref(false);
const tableData = ref([]);
const open = ref(false);
const total = ref(0);
const data = reactive({
  form: {
    examName: "",
    year: 2023,
    score: 0,
    questionUrl: "",
    answerUrl: "",
  },
  queryParams: {
    examName: undefined,
    year: undefined,
    pageNum: 1,
    pageSize: 10,
  },
});

const { queryParams, form } = toRefs(data);
// 查询
function viewQuestion(row) {
  Cookies.set("selectedExam", JSON.stringify(row));
  // 跳转编辑页面
  router.push("/exam/question");
}
function viewAnswer(row) {
  Cookies.set("selectedExam", JSON.stringify(row));
  // 跳转编辑页面
  router.push("/exam/answer");
}
/** 查询资源列表 */
function getList() {
  loading.value = true;
  queryList(queryParams.value).then((res) => {
    tableData.value = res.content;
    total.value = res.totalElements;
    loading.value = false;
  });
}
/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 新增
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
}
function handleDelete(row) {
  let id = row.id;
  deleteOne(id).then((r) => {
    proxy.$modal.msgSuccess("删除成功");
    getList();
  });
}
/** 提交按钮 */
function submitForm() {
  createOne(form.value).then((response) => {
    console.log(response);
    proxy.$modal.msgSuccess("新增成功");
    open.value = false;
    getList();
  });
}
getList();
</script>
<style></style>
